package com.cpecc.quartz;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cpecc.dao.wuzi.FenTanDao;
import com.cpecc.dao.wuzi.WuZiDao;
import com.cpecc.dao.wuzi.ZheJiuDao;
import com.cpecc.model.wuzi.FenTan;
import com.cpecc.model.wuzi.WuZi;
import com.cpecc.model.wuzi.ZheJiu;

@Service
public class ZheJiuSchedule {

	@Autowired
	private WuZiDao wuZiDao;

	@Autowired
	private FenTanDao fenTanDao;

	@Autowired
	private ZheJiuDao zheJiuDao;

	public void zheJiu() {
		// 当月不折旧
		List<WuZi> zheJiuWuZis = wuZiDao.getAllZheJiu();
		for (WuZi wuZi : zheJiuWuZis) {
			double wuZiYZ=wuZi.getWuZiYZ();
			double jingCanZhiLv=wuZi.getJingCanZhiLv();
			double leiJiZJJE=wuZi.getLeiJiZJJE();
			int zheJiuYX=wuZi.getZheJiuYX();
			int zheJiuYS=wuZi.getZheJiuYS();
			if (wuZi.getZheJiuFS().getNo() == 1) {
				double jingCanZhi = wuZiYZ * jingCanZhiLv / 100f;
				double zheJiuE = (wuZiYZ - leiJiZJJE - jingCanZhi)
						/ ((zheJiuYX - zheJiuYS) * 1.0f);
				if(zheJiuE>0){
					List<FenTan> fenTans = fenTanDao.getFenTanList(wuZi);
					if(fenTans.size()>0){
						for (FenTan fenTan : fenTans) {
							ZheJiu zheJiu = new ZheJiu();
							zheJiu.setFenTan(fenTan);
							zheJiu.setWuZi(wuZi);
							zheJiu.setZheJiuJE(zheJiuE);
							zheJiu.setZheJiuRQ(new Date());
							zheJiuDao.save(zheJiu);
						}
					}else{
						ZheJiu zheJiu = new ZheJiu();
						zheJiu.setWuZi(wuZi);
						zheJiu.setZheJiuJE(zheJiuE);
						zheJiu.setZheJiuRQ(new Date());
						zheJiuDao.save(zheJiu);
					}
					leiJiZJJE += zheJiuE;
					wuZi.setLeiJiZJJE(leiJiZJJE);
					wuZi.setWuZiJingZhi(wuZiYZ - leiJiZJJE);
					wuZi.setZheJiuYS(zheJiuYS + 1);
					wuZiDao.cover1(wuZi);
				}
			}
		}
		// 当月折旧
		List<WuZi> zheJiuWuZis2 = wuZiDao.getAllZheJiu2();
		for (WuZi wuZi : zheJiuWuZis2) {
			double wuZiYZ=wuZi.getWuZiYZ();
			double jingCanZhiLv=wuZi.getJingCanZhiLv();
			double leiJiZJJE=wuZi.getLeiJiZJJE();
			int zheJiuYX=wuZi.getZheJiuYX();
			int zheJiuYS=wuZi.getZheJiuYS();
			double jingCanZhi = wuZiYZ * jingCanZhiLv / 100f;
			double zheJiuE = (wuZiYZ - leiJiZJJE - jingCanZhi)
					/ ((zheJiuYX - zheJiuYS) * 1.0f);
			if(zheJiuE>0){
				List<FenTan> fenTans = fenTanDao.getFenTanList(wuZi);
				if(fenTans.size()>0){
					for (FenTan fenTan : fenTans) {
						ZheJiu zheJiu = new ZheJiu();
						zheJiu.setFenTan(fenTan);
						zheJiu.setWuZi(wuZi);
						zheJiu.setZheJiuJE(zheJiuE);
						zheJiu.setZheJiuRQ(new Date());
						zheJiuDao.save(zheJiu);
					}
				}else{
					ZheJiu zheJiu = new ZheJiu();
					zheJiu.setWuZi(wuZi);
					zheJiu.setZheJiuJE(zheJiuE);
					zheJiu.setZheJiuRQ(new Date());
					zheJiuDao.save(zheJiu);
				}
				leiJiZJJE += zheJiuE;
				wuZi.setLeiJiZJJE(leiJiZJJE);
				wuZi.setWuZiJingZhi(wuZiYZ - leiJiZJJE);
				wuZi.setZheJiuYS(zheJiuYS);
				wuZiDao.cover1(wuZi);
			}
		}
	}
}